<!-- 搜索框 -->
<template>
  <div class="wrapper">
    <div class="search-wrapper">
      <input
        type="text"
        class="searchInput"
        v-model="search"
        @keydown="searchList"
        @keyup="change"
        placeholder="搜索"
      />
      <i class="icon iconfont icon-search" v-show="noText"></i>
      <div class="searchInput-delete" v-show="haveText" @click="del"></div>
    </div>
    <div class="add_Plus" style="cursor: pointer" @click="createGroup"></div>
    <el-dialog title="创建群聊" :visible.sync="showAddmembers" width="300px">
      <div>
        <div style="display: flex; flex-direction: column">
          <el-input style="margin: 0 20px 20px 0" v-model="groupName" placeholder="请输入群名称"></el-input>
          <el-input v-model="description" placeholder="请输入群描述"></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="shure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions, mapGetters } from "vuex";
import axios from "axios";
export default {
  data() {
    return {
      showAddmembers: false,
      search: "",
      active: false,
      groupName: "",
      description: ""
    };
  },
  methods: {
    change() {
      this.$store.dispatch("search", this.search);
    },
    searchList(e) {
      var params = new FormData();
      params.append("name", this.search);
      params.append("memberId", this.$store.state.user.memberId);
      // if (e.keyCode == 13) {
      // 	axios.post("http://192.168.1.131:8080/wechatwebsite/index/getmembers.do",params).then(res=>{
      // 		console.log(res);
      // 	})
      // }
    },
    del() {
      this.search = "";
      this.change();
    },
    createGroup() {
      this.showAddmembers = true;
    },
    async shure() {
      let that = this;
      let params = new FormData();
      params.append("memberId", this.user.memberId);
      params.append("groupName", this.groupName);
      params.append("description ", this.description);

      const { data: object } = await this.$http.post(
        `/api/group/add.do`,
        params
      );
      // this.$http.post(`/api/group/add.do?memberId=${this.user.memberId}&groupName=${this.groupName}&description=${this.description}`, params)
      this.fetchGroupChatlist();
      this.showAddmembers = false;
    },

    async fetchGroupChatlist() {
      const { data } = await this.$http.get(
        `/api/groupMessage/getGroupByUserId.do?memberId=${this.user.memberId}`
      );
      this.$store.dispatch("groupList", data.object);
    },
    cancle() {
      this.showAddmembers = false;
    }
  },
  computed: {
    ...mapState(["selectId", "searchText", "user"]),
    noText() {
      if (this.search === "") return true;
      return false;
    },
    haveText() {
      if (this.search === "") return false;
      return true;
    }
  }
};
</script>

<style lang="stylus" scoped>
.wrapper {
  background-color: #ffffff;
  position: relative;
  padding: 22px 12px 12px 12px;
}

.add_Plus {
      width: 16px;
  height: 16px;
  background-image : url("../../assets/images/add.jpg");
  background-size : 100% 100%;
  position: absolute;
  top: 26px;
  right: 12px;
}

.search-wrapper {
  position: relative;
  display: flex;
  box-sizing: border-box;
  height: 26px;
  width: 90%;
  background-color: #e5e3e2;
  border: 1px solid #d9d7d6;
  border-radius: 2px;

  .searchInput {
    flex: 1;
    font-size: 12px;
    padding: 6px;
    background-color: #e5e3e2;
    outline: none;

    &:focus {
      background-color: #f2efee;
    }
  }

  .icon-search {
    display: inline-block;
    width: 24px;
    height: 24px;
    font-size: 14px;
    line-height: 24px;
    text-align: center;
  }

  .searchInput-delete {
    display: block;
    position: absolute;
    outline: none;
    top: 0;
    right: 0;
    width: 24px;
    height: 100%;
    background-image: url('delete.png');
    background-size: 26px;
    background-position: center;
    background-repeat: no-repeat;
    cursor: pointer;
  }
}
</style>
